package cn.zglbk.generator.scan;

import cn.zglbk.generator.utils.*;
import cn.zglbk.utils.CreateUtils;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipOutputStream;

public class CsGenerator {

    public static void generatorCode(List<CsClassModel<CsMethodModel>> entity,int type) {
        //配置信息
        Configuration config = getConfig();
        //添加到zip
        for (CsClassModel<CsMethodModel> tableEntity : entity) {

            generatorCode(tableEntity,  config,type);
        }
    }

    private static void generatorCode(CsClassModel<CsMethodModel> entity,  Configuration config,int type) {

        //设置velocity资源加载器x`
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        entity.setClazzName(StringUtils.tableToJava(entity.getClazzName(), config.getStringArray("tablePrefix")));
        VelocityContext context = new VelocityContext(wrappingMap(config, entity));
        //获取模板列表
        List<String> templates = getTemplates(type);
        for (String template : templates) {
            //渲染模板
            StringWriter sw = new StringWriter();
            Template tpl = Velocity.getTemplate(template, "UTF-8");
            tpl.merge(context, sw);
            System.out.println(sw.toString());
            String path = "XBProxy/";
            if(type == 2){
                path = "XBBroadcast/";
            }
            CreateUtils.create(entity.getClazzName() + ".cs", sw.toString(), config.getString("writePath") + path);

        }
    }

    private static Map<String, Object> wrappingMap(Configuration config, CsClassModel<CsMethodModel> entity) {
        //封装模板数据
        Map<String, Object> map = new HashMap<>();
        map.put("comments", entity.getComments());
        map.put("clazzName", entity.getClazzName());
        map.put("serviceId", entity.getServiceId());
        map.put("columns", entity.getColumns());
        map.put("namespace", config.getString("package"));
        map.put("moduleName", config.getString("moduleName"));
        map.put("author", config.getString("author"));
        map.put("email", config.getString("email"));
        map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        return map;
    }

    private static List<String> getTemplates(int type) {
        List<String> templates = new ArrayList<String>(1);
        if(type == 1){
            templates.add("template/CsController.java.vm");
        }else if(type == 2){
            templates.add("template/CsBroadcastBean.java.vm");
        }
        return templates;
    }

    /**
     * 获取配置信息
     */
    public static Configuration getConfig() {
        try {
            return new PropertiesConfiguration("generator.properties");
        } catch (ConfigurationException e) {
            throw new FsyException("获取配置文件失败，", e);
        }
    }
}
